//
//  LHNetworkManager+extension.swift
//  SinaWeibo
//
//  Created by 美食家 on 17/4/3.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import Foundation

extension LHNetWorkManager{
    
    /// 加载微博数据
    ///
    /// - parameter since_id:   返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0
    /// - parameter max_id:     返回ID小于或等于max_id的微博，默认为0。
    /// - parameter completion: 加载完成回调
    func statusList(since_id:Int64,max_id:Int64,completion:@escaping (_ list:[[String : AnyObject]]?,_ isSuccess:Bool)->()) -> Void {
        
        let urlString = "https://api.weibo.com/2/statuses/public_timeline.json"
        let params=["since_id":"\(since_id)",
            "max_id":"\(max_id>0 ? (max_id-1):0)"]
        tokenRequest(urlString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            let result=json?["statuses"] as? [[String:AnyObject]]
            completion(result,isSuccess)
        }
    }
    
    func unreadCount(completion:@escaping (_ count:Int)->()) {
        
        guard let uid=userAccount.uid else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let param=["uid":uid]
        
        tokenRequest(urlString: urlString, parameters: param as [String : AnyObject]?) { (json, isSuccess) in
            print("未读消息:\(json)")
            let dict = json as? [String : AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0 )
        }
    }
}

// MARK: - 加载用户信息
extension LHNetWorkManager{
    ///  加载用户信息
    func LoadUserInfo(completion: @escaping (_ dict : [String : AnyObject]) -> ()) {
        guard let uid = userAccount.uid else {
            return
        }
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid" : uid]
        
        tokenRequest(urlString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            
            completion(json as? [String : AnyObject] ?? [:])
            
        }
    }
}


// MARK: - 加载AccessToken
extension LHNetWorkManager{

    func loadAccessToken(code : String,completion:@escaping (_ isSuccess : Bool)->()){
    
        let urlString="https://api.weibo.com/oauth2/access_token"
        let params=["client_id" : WBAppKey,
                    "client_secret" : WBAppSecret ,
                    "grant_type" : "authorization_code",
                    "code" : code,
                    "redirect_uri":WBRedirectURI]
        request(Method: .POST, urlString: urlString, parameters: (params as [String : AnyObject]?)!) { (json, isSuccess) in
             //print("\(json)")
            self.userAccount.yy_modelSet(with: json as? [AnyHashable : Any] ?? [:])
            print("\(self.userAccount)")
            
            self.LoadUserInfo(completion: { (dict) in
                 self.userAccount.yy_modelSet(with: dict)
                 self.userAccount.saveAccount()
                print("用户信息：\(dict)")
                 completion(isSuccess)
            })
           
           
        }
    }
}
